/*********************************************************************************
  *Copyright (c)    2021   xldeng
  *FileName:        341.cpp.c
  *Author:          xldeng
  *Version:         1.0
  *Date:            2021/2/16 10:11
  *Description:     
  *Others:          
  *Function List:   
     1.…………
  *History:  
     1.Date:
       Author:
       Modification:
**********************************************************************************/
//给你一个嵌套的整型列表。请你设计一个迭代器，使其能够遍历这个整型列表中的所有整数。
//
//列表中的每一项或者为一个整数，或者是另一个列表。其中列表的元素也可能是整数或是其他列表。

// This is the interface that allows for creating nested lists.
// You should not implement it, or speculate about its implementation
#include "../header.h"

class NestedInteger {
public:
    // Return true if this NestedInteger holds a single integer, rather than a nested list.
    bool isInteger() const;

    // Return the single integer that this NestedInteger holds, if it holds a single integer
    // The result is undefined if this NestedInteger holds a nested list
    int getInteger() const;

    // Return the nested list that this NestedInteger holds, if it holds a nested list
    // The result is undefined if this NestedInteger holds a single integer
    const vector<NestedInteger> &getList() const;
};

class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        idx = 0;
        traverse(nestedList);
    }

    int next() {
        return result[idx];
    }

    bool hasNext() {
        return idx < result.size();
    }

private:
    vector<int> result;
    int idx;
    void traverse(const vector<NestedInteger> &vec) {
        for (auto &child : vec){
            if (child.isInteger())
                result.push_back(child.getInteger());
            else
                traverse(child.getList());
        }
    }
};

